using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCCornucopia.Data;
using MVCCornucopia.Models;
using MVCCornucopia.ViewModels;
using MVCCornucopia.Common;
using MVCCornucopia.Data.Enums;
using Links;

namespace MVCCornucopia.Controllers
{
    public partial class EmployeeController : Controller
    {
        private readonly IRepository repository;

        public EmployeeController(IRepository employeeRepository)
        {
            this.repository = employeeRepository;

            //-- setup automapper
            AutoMapper.Mapper.CreateMap<Gender, int>().ConvertUsing(new AutomapperHelper.GenderEnumTypeConverter());
            AutoMapper.Mapper.CreateMap<EmployeeOccupation, int>().ConvertUsing(new AutomapperHelper.EmployeeOccupationEnumTypeConverter());

            AutoMapper.Mapper.CreateMap<EmployeeVM, Employee>();
            AutoMapper.Mapper.CreateMap<Employee, EmployeeVM>();

        }


        public virtual ViewResult Index()
        {
            return View(repository.All);
            
        }

        public virtual PartialViewResult IndexAjax()
        {
            return PartialView(Views.IndexGrid, repository.All);
        }


        public virtual ViewResult Details(int id)
        {
            return View(repository.Find(id));
        }

        public virtual PartialViewResult DetailsAjax(int id)
        {
            return PartialView(Views.Details, repository.Find(id));
        }


        public virtual ActionResult Create()
        {
            return View();
        }

        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Create(EmployeeVM employeeVM)
        {
            if (ModelState.IsValid)
            {
                //set default
                if (!employeeVM.GenderId.HasValue)
                    employeeVM.GenderId = Gender.Male;

                //-- copy values from viewmodel to model
                Employee employee = (Employee)repository.Create();
                AutoMapper.Mapper.Map<EmployeeVM, Employee>(employeeVM, employee);

                if (employee.DateOfBirth == null || employee.DateOfBirth == DateTime.MinValue)
                    employee.DateOfBirth = DateTime.Now;

                //-- save the updated employee
                repository.InsertOrUpdate(employee);
                repository.Save();
                return RedirectToAction(ActionNames.Index);
            }
            else
            {
                return View();
            }
        }


        public virtual ActionResult Edit(int id)
        {
            var employee = (Employee)repository.Find(id);
            EmployeeVM em = new EmployeeVM();
            AutoMapper.Mapper.Map<Employee, EmployeeVM>(employee, em);
            return View(em);
        }


        public virtual PartialViewResult EditAjax(int id)
        {
            var employee = (Employee)repository.Find(id);
            EmployeeVM em = new EmployeeVM();
            AutoMapper.Mapper.Map<Employee, EmployeeVM>(employee, em);
            return PartialView("Edit", em);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Edit(EmployeeVM employeeVM)
        {

            
            if (ModelState.IsValid)
            {

                //-- copy values from viewmodel to model
                var employee = (Employee)repository.Find(employeeVM.EmployeeId);
                AutoMapper.Mapper.Map<EmployeeVM, Employee>(employeeVM, employee);

                if (employee.DateOfBirth == null || employee.DateOfBirth == DateTime.MinValue)
                    employee.DateOfBirth = DateTime.Now;

                //-- save the updated employee
                repository.InsertOrUpdate(employee);
                repository.Save();
                return RedirectToAction(ActionNames.Index);
            }
            else
            {
                return View();
            }
        }

        //[ValidateAntiForgeryToken]
        // TODO: demo validate antiforgery token from ajax
        public virtual JsonResult Delete(int id)
        {
            repository.Delete(id);
            repository.Save();
            return Json(true);            
        }


    }
}

